
package com.ivan.stu.notetool.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.Utils;
import com.github.zackratos.ultimatebar.UltimateBar;

import com.ivan.stu.notetool.R;
import com.ivan.stu.notetool.model.MySubjectBean;
import com.ivan.stu.notetool.model.MySystemBean;
import com.ivan.stu.notetool.model.OnSubjectChoosedEvent;
import com.ivan.stu.notetool.network.api.SafeApiManager;
import com.ivan.stu.notetool.network.intercepter.NetworkTransformer;
import com.ivan.stu.notetool.network.postbody.GetNoteSubjectPostBody;
import com.ivan.stu.notetool.network.postbody.GetSystemInfro;
import com.ivan.stu.notetool.network.response.MySubjectResponse;
import com.ivan.stu.notetool.network.response.MySystemInfro;
import com.ivan.stu.notetool.utils.ConstrantManager;
import com.ivan.stu.notetool.utils.TextViewUtils;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;
import com.zhy.view.flowlayout.TagView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.observers.ResourceObserver;


public class NoteSubjectActivity extends AppCompatActivity implements View.OnClickListener{

    private TagFlowLayout fl_subject,fl_system;
    private Button bt_commit;
    private ImageView iv_left_back;
    private TagAdapter<MySubjectBean> mSubjectAdapter;
    private TagAdapter<MySystemBean> mSystemAdapter;
    private List<MySubjectBean> subjectList= new ArrayList<>();
    private List<MySystemBean> systemBeans = new ArrayList<>();
    private String mCurrentChooseOfSubject = ConstrantManager.currnetNoteSubject;
    private String mCurrentChooseOfSubjectID = ConstrantManager.currentNoteSubjectID;
    private int subjectPosition = ConstrantManager.currentNoteSubjectPosition;
    private Handler mHandler = new Handler();
    private String mCurrentChooseOfSytemId = ConstrantManager.currentSystemid;
    private String mCurrentChooseOfSytemName = ConstrantManager.currentSystemName;
    private int systemPosition = ConstrantManager.currentSystemPostion;
    private TextView tv_system;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_note_subject);

        Utils.init(this);
        UltimateBar.Companion.with(this).applyNavigation(false).create().transparentBar();
        findViews();
        getNoteSubjectList();

        if (ConstrantManager.currentSystemid.equals("S21")){

            fl_system.setVisibility(View.VISIBLE);
            tv_system.setVisibility(View.VISIBLE);

            getSystemInfro();

        }else {

            fl_system.setVisibility(View.GONE);
            tv_system.setVisibility(View.GONE);

        }

    }

    private void getSystemInfro() {

        GetSystemInfro infro = new GetSystemInfro();
        infro.setBackUpOne("");
        infro.setBackUpTwo("");
        infro.setSecretKey("");
        infro.setToken(ConstrantManager.Token);
        infro.setUserID(ConstrantManager.childUserID);
        infro.setUserType(ConstrantManager.userType);


        SafeApiManager.getTestApi(3).getSystemInfo(infro)
                .compose(NetworkTransformer.<MySystemInfro>commonSchedulers())
                .subscribe(new ResourceObserver<MySystemInfro>() {
                    @Override
                    public void onNext(MySystemInfro mySystemInfro) {


                        if (mySystemInfro.Result!=null&&mySystemInfro.Result.size()>0){

                            initSystemFlowLayout(mySystemInfro.Result);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                        TextViewUtils.showToast(NoteSubjectActivity.this,0,e.toString());

                    }

                    @Override
                    public void onComplete() {



                    }
                });

   /*     mSystemSub = SafeApiManager.getTestApi(3).getSystemInfo(infro)
                .compose(NetworkTransformer.<MySystemInfro>commonSchedulers())
                .subscribe(new Subscriber<MySystemInfro>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        TextViewUtils.showToast(NoteSubjectActivity.this,0,e.toString());

                    }

                    @Override
                    public void onNext(MySystemInfro mySystemInfro) {

                        if (mySystemInfro.Result!=null&&mySystemInfro.Result.size()>0){

                            initSystemFlowLayout(mySystemInfro.Result);
                        }
                    }
                });*/
    }


    private void getNoteSubjectList(){


        GetNoteSubjectPostBody body  = new GetNoteSubjectPostBody();

        body.setBackUpOne("");
        body.setBackUpTwo("");
        body.setSchoolID(ConstrantManager.childSchoolID);
        body.setToken(ConstrantManager.Token);
        body.setBackUpOne("");
        body.setUserID(ConstrantManager.childUserID);
        body.setUserType(ConstrantManager.userType);



        SafeApiManager.getTestApi(3).getNoteSubjectList(body)
                .compose(NetworkTransformer.<MySubjectResponse>commonSchedulers())
                .subscribe(new ResourceObserver<MySubjectResponse>() {
                    @Override
                    public void onNext(MySubjectResponse mySubjectResponse) {

                        if (mySubjectResponse.Result!=null&&mySubjectResponse.Result.size()>0){

                            subjectList.clear();
                            subjectList.addAll(mySubjectResponse.Result);

                            if (subjectList!=null && subjectList.size()>0){

                                initSubjectFlowLayout();
                            }


                        }

                    }

                    @Override
                    public void onError(Throwable e) {

                        TextViewUtils.showToast(NoteSubjectActivity.this,0,e.toString());

                    }

                    @Override
                    public void onComplete() {

                    }
                });


     /*   mSubjectSub = SafeApiManager.getTestApi(3).getNoteSubjectList(body)
                .compose(NetworkTransformer.<MySubjectResponse>commonSchedulers())
                .subscribe(new Subscriber<MySubjectResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                        TextViewUtils.showToast(NoteSubjectActivity.this,0,e.toString());

                    }

                    @Override
                    public void onNext(MySubjectResponse mySubjectResponse) {

                        if (mySubjectResponse.Result!=null&&mySubjectResponse.Result.size()>0){

                            subjectList.clear();
                            subjectList.addAll(mySubjectResponse.Result);

                            if (subjectList!=null && subjectList.size()>0){

                                initSubjectFlowLayout();
                            }


                        }
                    }
                });*/
    }

    private void  initSystemFlowLayout(List<MySystemBean> systems){

        systemBeans.clear();
        systemBeans.addAll(systems);

        final LayoutInflater mInflater = LayoutInflater.from(this);
        mSystemAdapter = new TagAdapter<MySystemBean>(systemBeans)
        {

            @Override
            public View getView(com.zhy.view.flowlayout.FlowLayout parent, int position, MySystemBean s)
            {

                TextView tv = (TextView) mInflater.inflate(R.layout.subject_item_layout,
                        fl_system, false);

                Log.e("systembean",systemBeans.get(position).getSystemName());

                tv.setText(s.getSystemName());

                return tv;
            }

            @Override
            public boolean setSelected(int position, MySystemBean s)
            {
                return false;
            }
        };

        fl_system.setAdapter(mSystemAdapter);
        ((TagView)fl_system.getChildAt(systemPosition)).setChecked(true);

        fl_system.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {

                if (((TextView)view).getText().toString().equals(mCurrentChooseOfSytemName)){

                    mCurrentChooseOfSytemName = ((TextView)view).getText().toString();
                    mCurrentChooseOfSytemId = systemBeans.get(position).getSystemID();
                    ConstrantManager.currentSystemid = systemBeans.get(position).getSystemID();
                    ConstrantManager.currentSystemName = ((TextView)view).getText().toString();
                    systemPosition = position;
                    ConstrantManager.currentSystemPostion = position;
                    ((TagView)fl_system.getChildAt(position)).setChecked(true);

                }else {

                    for (int i=0;i<fl_system.getChildCount();i++){

                        if (i == position){

                            ((TagView)fl_system.getChildAt(i)).setChecked(true);

                        }else {

                            ((TagView)fl_system.getChildAt(i)).setChecked(false);

                        }
                    }

                    mCurrentChooseOfSytemName = ((TextView)view).getText().toString();
                    mCurrentChooseOfSytemId= systemBeans.get(position).getSystemID();

                    Log.e("mCurrentSytemName",mCurrentChooseOfSytemName+"");
                    Log.e("mCurrentChooseOfSytemId",mCurrentChooseOfSytemId+"");
                    ConstrantManager.currentSystemid= systemBeans.get(position).getSystemID();
                    ConstrantManager.currentSystemName = ((TextView)view).getText().toString();
                    systemPosition = position;
                    ConstrantManager.currentSystemPostion = position;
                }

                return true;
            }
        });
    }


    private void initSubjectFlowLayout() {

        final LayoutInflater mInflater = LayoutInflater.from(this);
        mSubjectAdapter = new TagAdapter<MySubjectBean>(subjectList)
        {

            @Override
            public View getView(com.zhy.view.flowlayout.FlowLayout parent, int position, MySubjectBean s)
            {

                TextView tv = (TextView) mInflater.inflate(R.layout.subject_item_layout,
                        fl_subject, false);

                tv.setText(s.getSubjectName());

                return tv;
            }

            @Override
            public boolean setSelected(int position, MySubjectBean s)
            {
                return false;
            }
        };

        fl_subject.setAdapter(mSubjectAdapter);
        ((TagView)fl_subject.getChildAt(subjectPosition)).setChecked(true);

        fl_subject.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
           @Override
           public boolean onTagClick(View view, int position, FlowLayout parent) {

              if (((TextView)view).getText().toString().equals(mCurrentChooseOfSubject)){

                   mCurrentChooseOfSubject = ((TextView)view).getText().toString();
                   mCurrentChooseOfSubjectID = subjectList.get(position).getSubjectID();
                   ConstrantManager.currentNoteSubjectID = subjectList.get(position).getSubjectID();
                   ConstrantManager.currnetNoteSubject = ((TextView)view).getText().toString();
                   subjectPosition = position;
                   ConstrantManager.currentNoteSubjectPosition = position;
                   ((TagView)fl_subject.getChildAt(position)).setChecked(true);

               }else {

                   for (int i=0;i<fl_subject.getChildCount();i++){

                       if (i == position){

                           ((TagView)fl_subject.getChildAt(i)).setChecked(true);

                       }else {

                           ((TagView)fl_subject.getChildAt(i)).setChecked(false);

                       }
                   }

                  mCurrentChooseOfSubject = ((TextView)view).getText().toString();
                  mCurrentChooseOfSubjectID = subjectList.get(position).getSubjectID();
                  ConstrantManager.currentNoteSubjectID = subjectList.get(position).getSubjectID();
                  ConstrantManager.currnetNoteSubject = ((TextView)view).getText().toString();
                  subjectPosition = position;
                  ConstrantManager.currentNoteSubjectPosition = position;

               }

               return true;
           }
       });

    }

    private void findViews() {

        fl_subject = findViewById(R.id.id_subject_flowlayout);
        iv_left_back = findViewById(R.id.iv_left_back);
        bt_commit = findViewById(R.id.bt_commit);
        fl_system = findViewById(R.id.id_system_flowlayout);
        bt_commit.setOnClickListener(this);
        iv_left_back.setOnClickListener(this);
        tv_system = findViewById(R.id.tv_system);
    }

    @Override
    public void onClick(View view) {
        int i = view.getId();
        if (i == R.id.iv_left_back) {
            finish();

        } else if (i == R.id.bt_commit) {
            OnSubjectChoosedEvent event = new OnSubjectChoosedEvent();
            event.setSubjectID(mCurrentChooseOfSubjectID);
            event.setSystemID(mCurrentChooseOfSytemId);
            event.setSystemName(mCurrentChooseOfSytemName);
            EventBus.getDefault().post(event);


            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {

                    finish();

                }
            }, 0);


        } else {
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
